package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.CompanyBranch;

public class CompanyBranchDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public CompanyBranchDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public int createOrUpdateFCompanyBranch(ArrayList<CompanyBranch>  list) {
		int count =0;		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{

		for (CompanyBranch branch : list) {
			
		ContentValues values = new ContentValues();
		values.put(dbHelper.FCOMPANYBRANCH_BRANCH_CODE,  branch.getFCOMPANYBRANCH_BRANCH_CODE());
		values.put(dbHelper.FCOMPANYBRANCH_RECORD_ID,  branch.getFCOMPANYBRANCH_RECORD_ID());
		values.put(dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE,  branch.getFCOMPANYBRANCH_CSETTINGS_CODE());
		values.put(dbHelper.FCOMPANYBRANCH_NNUM_VAL,  branch.getFCOMPANYBRANCH_NNUM_VAL());
								
		count = (int) dB.insert(dbHelper.TABLE_FCOMPANYBRANCH, null, values);
	
		}
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	@SuppressWarnings("static-access")
	public String getCurrentNextNumVal(String cSettingsCode){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FCOMPANYBRANCH +" WHERE "+dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE+" ='"+cSettingsCode+"'";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FCOMPANYBRANCH_NNUM_VAL));
			
		}
			
		return "0";
	}
	
	@SuppressWarnings("static-access")
	public int deleteAll(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FCOMPANYBRANCH, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FCOMPANYBRANCH, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	



}
